<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/12
 * Time: 11:45
 */

namespace zhtfrontend\components\services\goods;
use common\components\helpers\Fun;
use yii\base\Action;
use yii\web\BadRequestHttpException;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\ReportRecord;
use yii\widgets\ActiveForm;
use yii\web\Response;


class ReportAction extends Action{
    public $type = 1;
    public function run(){
        $this->controller->layout='@app/views/layouts/iframe.php';
        if(\Yii::$app->request->get('id') == null){
            throw new BadRequestHttpException(\Yii::t('frontend','商品id为空'));
        }
        $goods_id = \Yii::$app->request->get('id');
        $goods = Goods::findOne($goods_id);
        $model = new ReportRecord();
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->module_id = $goods_id;
            $model->sender_id = \Yii::$app->user->id;
            $head = Admin::find()->where(['mechanic_id'=>$goods->mechanic_id,'role'=>'mec_principal','status'=>1])->one();
            $model->sendee_id = isset($head->id)?$head->id:0;
            $model->stage = 1;
            $model->type = 1;
            $model->status = 0;
            if(\Yii::$app->user->identity->role == 'mechanic') {
                $model->mechanicremark = $model->remark;
                $model->operator_type = 'mechanic';
            }else {
                $model->workerremark = $model->remark;
                $model->operator_type = 'square';
            }
            $model->is_visual = 0;
            if ($model->save(false)) {
                $goods->status = 2;
                $goods->save(false);
                $url = $this->controller->id;
                $url .= '/index';
                return $this->controller->redirectMessage('提报成功', Fun::url([$url]));
            }
        }
        return $this->controller->render('@zhtfrontend/components/services/goods/report.php',['model' => $model]);
    }
}
?>